package com.kunkun.auth.domain.service.Impl;

import com.kunkun.auth.common.enums.AuthUserStatusEnum;
import com.kunkun.auth.common.enums.IsDeletedFlagEnum;
import com.kunkun.auth.domain.bo.AuthPermissionBO;
import com.kunkun.auth.domain.bo.AuthRolePermissionBO;
import com.kunkun.auth.domain.bo.AuthUserBO;
import com.kunkun.auth.domain.convert.AuthPermissionBOConverter;
import com.kunkun.auth.domain.convert.AuthRolePermissionBOConverter;
import com.kunkun.auth.domain.redis.RedisUtil;
import com.kunkun.auth.domain.service.AuthPermissionDomainService;
import com.kunkun.auth.domain.service.AuthRolePermissionDomainService;
import com.kunkun.auth.infra.basic.entity.AuthPermission;
import com.kunkun.auth.infra.basic.entity.AuthRolePermission;
import com.kunkun.auth.infra.basic.service.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthUserService authUserService;

    @Resource
    private AuthRoleService authRoleService;

    @Resource
    private AuthUserRoleService authUserRoleService;

    @Resource
    private AuthRolePermissionService authRolePermissionService;

    @Resource
    private AuthPermissionService authPermissionService;


    private String salt = "chicken";

    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix = "auth.permission";

    private String authRolePrefix = "auth.role";

    private static final String LOGIN_PREFIX = "loginCode";




    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        List<AuthRolePermission> rolePermissionList = new LinkedList<>();
        Long roleId = authRolePermissionBO.getRoleId();
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            rolePermissionList.add(authRolePermission);

        });
        Integer count = authRolePermissionService.batchInsert(rolePermissionList);
        return count > 0;
    }




}
